# Initialize the OpenAI language model
# Replace <your_api_key> in openai_api_key="<your_api_key>" with your actual OpenAI key.
from langchain.agents import initialize_agent, AgentType
from langchain.chat_models import ChatOpenAI
from langchain.globals import set_debug
from langchain.tools import Tool
from langchain.utilities.serpapi import SerpAPIWrapper

llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613")

# Initialize the SerpAPIWrapper for search functionality
# Replace <your_api_key> in serpapi_api_key="<your_api_key>" with your actual SerpAPI key.
search = SerpAPIWrapper()

# Define a list of tools offered by the agent
tools = [
    Tool(
        name="Search",
        func=search.run,
        description="Useful when you need to answer questions about current events. You should ask targeted questions.",
    ),
]
mrkl = initialize_agent(
    tools, llm, agent=AgentType.OPENAI_MULTI_FUNCTIONS, verbose=True
)
# 这样做是为了让我们能够准确地看到幕后发生的事情
set_debug(True)
mrkl.run("What is the weather in LA and SF?")
# 为了确保我们的代理不会陷入过长的循环中，我们可以设置 max_iterations。我们还可以设置提前停止方法，一旦达到最大迭代次数，
# 该方法将确定代理的行为。默认情况下，早期停止使用强制方法，该方法仅返回该常量字符串。或者，您可以指定方法generate，
# 然后该方法通过LLM进行最后一次传递以生成输出
mrkl = initialize_agent(
    tools,
    llm,
    agent=AgentType.OPENAI_FUNCTIONS,
    verbose=True,
    max_iterations=2,
    early_stopping_method="generate",
)
mrkl.run("What is the weather in NYC today, yesterday, and the day before?")